import datetime
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np
from matplotlib.axes import Axes
from matplotlib.dates import DateFormatter, WeekdayLocator, MONDAY, MonthLocator
from sklearn import preprocessing

pd.set_option('display.height', 1000)
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
plt.rcParams.update({'figure.autolayout': True})

na_black_friday = ['2014-11-28',
                   '2015-11-27',
                   '2016-11-25',
                   '2017-11-24',
                   '2018-11-30']
na_labor_day = ['2014-09-01',
                '2015-09-07',
                '2016-09-05',
                '2017-09-04',
                '2018-09-03']
emea_kaizhaijie = ['2014-07-29',
                   '2015-07-18',
                   '2016-07-05',
                   '2017-06-26',
                   '2018-06-15']

emea_guerbangjie = ['2014-10-04',
                    '2015-09-24',
                    '2016-09-13',
                    '2017-09-02',
                    '2018-08-22']
prc_double11 = ['2014-11-11',
                '2015-11-11',
                '2016-11-11',
                '2017-11-11',
                '2018-11-11']
prc_chinesenewyear = ['2014-02-01',
                      '2015-02-18',
                      '2016-02-07',
                      '2017-01-28',
                      '2018-02-16']
india_diwali_start = ['2014-10-23',
                      '2015-11-11',
                      '2016-10-30',
                      '2017-10-18',
                      '2018-11-06']
india_diwali_end = ['2014-10-27',
                    '2015-11-15',
                    '2016-11-03',
                    '2017-10-22',
                    '2018-11-10']
christmas = ['2014-12-25',
             '2015-12-25',
             '2016-12-25',
             '2017-12-25',
             '2018-12-25']
# --
# ----
sub_geo = 'INDIA'
# geo = 'NA'
# business_type = 'Consumer'  # 'Commercial'
year_range = 2016
holiday_black = india_diwali_start
holiday_red = india_diwali_end
holiday_green = christmas
schemas = 'PORTFOLIO'
# ------------------- start reading -------------------------------------------
df = pd.read_csv('D:/!Python/helloworld2/feature_data_20171113_1_2.csv')
df_g = pd.read_csv('data/family_group.csv')
values = {'geo': 'NA',
          'sub_geo': 'NA',
          'date': '1989-01-01',
          'original_quantity': 1}
df = df.fillna(value=values)

# -- 分类 --
# df = df[df['geo'] == geo]
df = df[df['sub_geo'] == sub_geo]
# df = df[df['business_type'] == business_type]

df = df[['sub_geo', 'family_desc', 'geo', 'group_id', 'date', 'original_quantity', 'business_type']]
assert isinstance(df, pd.DataFrame)
df_g = df_g[['family_desc', 'PORTFOLIO', 'LEVEL', 'CATEGORY']]
df_m = df.merge(df_g, 'left', 'family_desc')
# print(df_m)

# df['new_col'] = df['sub_geo'] + '@' + df['group_id'] + '@' + df['family_desc']
# df = df[['new_col', 'date', 'original_quantity']]
df = df_m
df['new_col'] = df[schemas]
# print(df['new_col'])
# exit(0)


df = df[['date', 'new_col', 'original_quantity']]
df = df.groupby(['date', 'new_col'], as_index=False).sum()

# ============================
type_list = df['new_col'].dropna().unique().tolist()
f, tuple_ax = plt.subplots(figsize=(10, 8), nrows=len(type_list))
print(tuple_ax)
i = 0
for ax in tuple_ax:
    print(type(ax))
    # assert isinstance(ax, Axes)
    df_m = df[df['new_col'] == type_list[i]]

    df_s_index_list = list(map(lambda xx: datetime.datetime.strptime(xx, '%Y-%m-%d'),
                               df_m['date'].values.tolist()))
    df_s_values = df_m['original_quantity'].values.tolist()
    ax.plot(df_s_index_list, df_s_values)

    for day in holiday_black:  # 添加黑色节日
        datetime_day = datetime.datetime.strptime(day, '%Y-%m-%d')
        ax.axvline(datetime_day, label="1", color='black', linestyle="--")  # hold=None
    for day in holiday_red:  # 添加红色节日
        datetime_day = datetime.datetime.strptime(day, '%Y-%m-%d')
        ax.axvline(datetime_day, label="1", color='red', linestyle="--")  # hold=None

    if holiday_green is not None:
        for day in holiday_green:  # 添加红色节日
            datetime_day = datetime.datetime.strptime(day, '%Y-%m-%d')
            ax.axvline(datetime_day, label="1", color='orange', linestyle="--")  # hold=None

    ax.xaxis.set_major_locator(MonthLocator(interval=2))
    min_date = datetime.datetime.now().replace(2014, 1, 1, 0, 0, 0)
    max_date = datetime.datetime.now().replace(2019, 1, 1, 0, 0, 0)
    ax.set(xlim=[min_date, max_date], ylabel=type_list[i])
    yearsFmt = DateFormatter('%Y-%m')
    ax.xaxis.set_major_formatter(yearsFmt)  # 设置主要时间显示格式(日期类型的格式化)
    labels = ax.get_xticklabels()
    plt.setp(labels, rotation=45, horizontalalignment='right')  # 标签是竖着排、还是横着排、还是斜着排
    i += 1
fig = plt.figure(1)
fig.set_size_inches(20, 4.5 * len(type_list))
plt.tight_layout()
# plt.show()
plt.savefig('output/' + schemas + ".jpg")
exit(0)
# ============================

# print(df)
# exit(0)

df = df.drop_duplicates()
df = df.pivot('date', 'new_col', 'original_quantity')  # 生成透视图
df = df.fillna(0)

x = df.values  # returns aa numpy array
# ---------- 进行归一化处理 ----------------------
# ---- 准备热力图数据 ----
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
df_c = df.columns
df = pd.DataFrame(x_scaled, index=df.index.values, columns=df.columns)

# ---- 准备归一化图数据
df_s = df.stack()
df_ss = df_s.copy()

df_s = df_s.groupby(df_s.index.get_level_values(0)).sum()
df_s_index_list_1 = list(map(lambda xx: datetime.datetime.strptime(xx, '%Y-%m-%d'), df_s.index.values.tolist()))
df_s_values_1 = df_s.values.tolist()

# ---- 生成同一年的合并对比数据 ----
# step 1: 对归一化数据进行铺平
assert isinstance(df_ss, pd.Series)
index_v = df_ss.index.values.tolist()
value = df_ss.values.tolist()
value_list = []
for i in range(0, len(value)):
    # print(datetime.datetime.strptime(index_v[i][0], '%Y-%m-%d'))
    content = [datetime.datetime.strptime(index_v[i][0], '%Y-%m-%d'),
               index_v[i][1],
               value[i]]
    # print(content)
    value_list.append(content)
df_ss = pd.DataFrame(value_list, columns=['date', 'new_col', 'quantity'])
# ---- 对归一化的数据进行铺平整理完毕 ----

df_tmp = df_ss

df1 = df_tmp[(df_tmp.date >= (str(year_range) + "-01-01")) &
             (df_tmp.date < (str(year_range + 1) + "-01-01"))]
# print(df1, df1.dtypes)
df2 = df_tmp[(df_tmp.date >= (str(year_range + 1) + "-01-01")) &
             (df_tmp.date < (str(year_range + 2) + "-01-01"))]

df1_tmp = df1.copy()
df1['date'] = df1_tmp['date'] + datetime.timedelta(days=364)

df_m = df1.merge(df2, 'outer', ['date', 'new_col'])
df_m = df_m.fillna(0)
df_m['quantity'] = df_m['quantity_x'] + df_m['quantity_y']

# ------------- 对每个date进行sum ---------------------
df_m = df_m.groupby(df_m['date']).sum()
print(df_m)
print(df_m.dtypes)

# exit(0)
df_s_index_list = df_m.index.values.tolist()
df_s_index_list = list(map(lambda xx: datetime.datetime.fromtimestamp(xx / 1000000000), df_s_index_list))
# print(df_s_index_list)
# exit(0)
df_s_values = df_m['quantity'].values.tolist()
# print(df_s_index_list)
# exit(0)
# -------------- 进行图像绘制 -----------------
# ------------- 绘制归一化图 ----------------
f, (ax2, ax1) = plt.subplots(figsize=(10, 8), nrows=2)
ax1.plot(df_s_index_list, df_s_values)
for day in holiday_black:  # 添加黑色节日
    datetime_day = datetime.datetime.strptime(day, '%Y-%m-%d')
    ax1.axvline(datetime_day, label="1", color='black', linestyle="--")  # hold=None
for day in holiday_red:  # 添加红色节日
    datetime_day = datetime.datetime.strptime(day, '%Y-%m-%d')
    ax1.axvline(datetime_day, label="1", color='red', linestyle="--")  # hold=None
ax1.axhline(0, label="1", color='green', linestyle="--")
yearsFmt = DateFormatter('%Y-%m-%d')
ax1.xaxis.set_major_locator(MonthLocator(interval=2))
ax1.xaxis.set_major_formatter(yearsFmt)  # 设置主要时间显示格式(日期类型的格式化)
ax1.xaxis.set_minor_locator(WeekdayLocator(MONDAY))  # 设置次要时间间隔!!!!! 成功
df_s_index_list_1.sort()
min_date = df_s_index_list_1[0]
max_date = df_s_index_list_1[len(df_s_index_list_1) - 1]
ax1.set(xlim=[min_date, max_date])
labels = ax1.get_xticklabels()
plt.setp(labels, rotation=45, horizontalalignment='right')  # 标签是竖着排、还是横着排、还是斜着排

# ------------ 绘制热力图 -------------------
plt.close()
df = df.T
ax2 = sns.heatmap(df, center=0, cbar=True)  # ax=ax2
labels = ax2.get_xticklabels()
plt.setp(labels, rotation=45, horizontalalignment='right')
ax2.set(xlabel='datetime', ylabel='family_subgeo',
        title='heat_map')
plt.show()
